<?php
/**
 * 线路订单回调
 */
namespace app\common\model\order;

use app\common\model as M;
class RoutesBook extends OrderAbtract
{
    /**
     * [paid_callback description]
     * @Author   FW
     * @DateTime 2021-09-18
     * 勿忘国耻，吾辈自强。
     * @Email    123763712@qq.com
     * @param    [type]           $order [description]
     * @return   [type]                  [description]
     */
    public function paid_callback( $order )
    {
        $outerStatus = M\OrderOuterRoutes::TYPE_UNCONFIRM;
        if ( $order->status == M\Order::STATUS_DEPOSIT ) {
            $outerStatus = M\OrderOuterRoutes::TYPE_DEPOSIT;
        }
        $outer = M\OrderOuterRoutes::where('id',$order->order_outer_id)->find();
		if ( empty($outer) ) throw new \Exception('线路预定信息错误【0092】');
        $outer->status = $outerStatus;
        $res = $outer->save();
        if ( $res === false ) throw new \Exception('网络错误，请稍候再试【0092】');

        if ( $order->status == M\Order::STATUS_VALID ) {
    		$sPrice = M\RoutesSkuPrice::where(['route_id' => $outer->route_id,'sku_id' => $outer->sku_id , 'date' => $outer->date])->find();
            $sPrice->left_store -= $outer->nums;
            $sPrice->double_room -= $outer->double_room;
            $sPrice->big_room -= $outer->big_room;
    		$res = $sPrice->save();
            if ( $res === false ) throw new \Exception('网络错误，请稍候再试【0092】');

            ## 检查是否返金币
            $is_return_gold = M\Routes::where('id',$outer->route_id)->value('is_return_gold');
            if ( $is_return_gold == 1 ) {
                $golds = ceil($order->order_amt / 100);
                $res = M\user\UserGoldsBill::add($amt = $golds , $stype = M\user\UserGoldsBill::STYPE_BUY , $uid = $order->uid);
                if ( $res !== true ) throw new \Exception('网络错误，请稍候再试【0094】');
            }
        }
        return true;
    }

    /** 获取订单编号前缀 */
    protected static function get_order_no_prefix()
    {
        return 'RO';
    }

    /** 获取订单类型 */
    protected static function get_order_type()
    {
        return \app\common\model\Order::ORDER_TYPE_ROUTES;
    }
} //Class End